﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;

public partial class MasterPage : System.Web.UI.MasterPage
{
    public int departId;
    public int positionId;
    public int employeeId;
    public int permissions;
    
    protected void Page_Load(object sender, EventArgs e)
    {
        permissions = int.Parse(Session["permissions"].ToString());

        if (permissions != 1)
        {
            departId = int.Parse(Session["departID"].ToString());
            positionId = int.Parse(Session["positionID"].ToString());
            employeeId = int.Parse(Session["employeeID"].ToString());
        }
        if (!IsPostBack)
        {
            ShowPannel();        
        }
    }


    private void ShowPannel()
    {
        if (permissions == 1)
        {
            HyperLink1.Visible = true;
            lbtn_Name.Enabled = false;
        }
        else
        {
            HyperLink1.Visible = false;
            lbtn_Name.Enabled = true;
        }
        lbtn_Name.Text = getUerName();

    }


    /// <summary>
    /// 创建数据库连接
    /// </summary>
    /// <returns></returns>
    private SqlConnection CreateConn()
    {
        string connStr = ConfigurationManager.ConnectionStrings["ConStr"].ToString();
        SqlConnection conn = new SqlConnection(connStr);

        return conn;
    }

    /// <summary>
    /// 获得员工的姓名
    /// </summary>
    /// <returns></returns>
    private string getUerName()
    {
        string Name = "";
        if (permissions == 1)
        {
            Name = "管理员";
            return Name;
        }
        else
        {
            SqlConnection conn = CreateConn();
            string selectCmdStr = "SELECT Name FROM tb_EmployeeInfo WHERE ID='" + employeeId + "'";

            try
            {
                SqlCommand selectCmd = new SqlCommand(selectCmdStr, conn);
                conn.Open();

                /*SqlDataReader da = selectCmd.ExecuteReader();
                Name = da.ToString();*/

                SqlDataAdapter da = new SqlDataAdapter();
                da.SelectCommand = selectCmd;

                DataSet ds = new DataSet();
                da.Fill(ds);

                Name = ds.Tables[0].Rows[0][0].ToString();

                conn.Close();
            }
            catch (Exception ex)
            {
                conn.Close();
            }

            return Name;
        }
    }

    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        Session.Clear();
        Response.Redirect("Login.aspx");
    }
    protected void lbtn_EditPwd_Click(object sender, EventArgs e)
    {
        Response.Redirect("ModifyPwd.aspx");
    }
    protected void lbtn_Name_Click(object sender, EventArgs e)
    {
        string temp = string.Format("EDetaeil.aspx?ID={0}", employeeId);
        Response.Redirect(temp);
    }
}
